package com.team.merchant.service.impl;

import com.team.merchant.config.CacheSyncConfig;
import com.team.merchant.domain.AccountFlow;
import com.team.merchant.domain.OrderInfo;
import com.team.merchant.domain.vo.RabbitDetailVo;
import com.team.merchant.domain.vo.RecordVo;
import com.team.merchant.mapper.WxVerificationMapper;
import com.team.merchant.service.IWxVerificationService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class WxVerificationServiceImpl implements IWxVerificationService {
    @Autowired
    private WxVerificationMapper wxVerificationMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    @Transactional
    public AccountFlow verification(String verificationCode,Long merchantId) {
        //进行核销，首先要到biz_order表当中查看是否已支付且未核销，然后都满足后，向biz_account_flow表当中插入一条数据，然后再更新biz_order表当中的核销状态
        List<OrderInfo> orderInfos = wxVerificationMapper.checkCode(verificationCode);
        if(orderInfos.size() <= 0){
            return null;
        }
        // 开始进行核销
    //获取列表第一个元素,sql查询到的都是待核销和已支付的订单，可以直接进行核销
        OrderInfo orderInfo = orderInfos.get(0);
    //开始向商户流水表当中插入一条数据
        // 插入数据,先构造好插入的对象，为其赋值，然后调用mapper层的insert方法
        AccountFlow accountFlow = new AccountFlow();
        Long id = orderInfo.getId();
        accountFlow.setOrderId(id);
        accountFlow.setMerchantId(merchantId);
        accountFlow.setType(0);//0代表收入
        accountFlow.setAmount(orderInfo.getPrice());
        accountFlow.setSource(orderInfo.getServiceTypeId());
        accountFlow.setFlowTime(new Date());
        int i = wxVerificationMapper.insertFlow(accountFlow);
        //如果i<=0,说明插入失败，直接返回
        if(i <= 0){
            return null;
        }
        //如果i>0,说明插入成功，开始更新订单表当中的核销状态
        int update = wxVerificationMapper.updateOrderStatus(id);
        if(update <= 0){
            return null;
        }
        //此时数据库更改完成，返回1
        return accountFlow;
    }

    @Override
    //生产者进程，向交换机当中发送消息
    public void sendOrderMessage(AccountFlow accountFlow) {
        RabbitDetailVo rabbitDetailVo=new RabbitDetailVo();
        rabbitDetailVo.setOrderId(accountFlow.getOrderId());
        rabbitDetailVo.setAmount(accountFlow.getAmount());
        rabbitDetailVo.setOperation("核销");
        rabbitDetailVo.setTimestamp(new Date());
        String s = wxVerificationMapper.searchServiceType(accountFlow.getSource());
        rabbitDetailVo.setServiceType(s);
        
        System.out.println("=== 发送核销消息到MQ ===");
        System.out.println("消息内容: " + rabbitDetailVo);
        System.out.println("发送到交换机: " + CacheSyncConfig.MERCHANT_EXCHANGE);
        System.out.println("使用路由键: " + CacheSyncConfig.MERCHANT_ROUTING_KEY);
        
        // 发送到商户专用交换机和路由键
        rabbitTemplate.convertAndSend(CacheSyncConfig.MERCHANT_EXCHANGE, CacheSyncConfig.MERCHANT_ROUTING_KEY, rabbitDetailVo);
        System.out.println("=== 消息已发送 ===");
    }

    @Override
    public List<RecordVo> getRecord() {
        return wxVerificationMapper.selectRecord();
    }

    @Override
    public Long getMerchantId(Long userId) {
        return wxVerificationMapper.getMerchantId(userId);
    }
}
